# Built-In Example AI

# Title: DirectTurtle
# Author: Adam Rumpf
# Version: 1.0.1
# Date: 11/13/2020

import game.tcturtle
import math
import random

class CombatTurtle(game.tcturtle.TurtleParent):
    # 正式版
    """Direct combat turtle.

    Its main strategy is to try to move directly towards the opponent, firing
    missiles when it has clear line of sight. It does not pay much attention
    to obstacles.
    """

    # -------------------------------------------------------------------------

    def class_name():
        """CombatTurtle.class_name() -> str
        Static method to return the name of the Combat Turtle AI.
        """

        return "rg_cfpolang"

    # -------------------------------------------------------------------------

    def class_desc():
        """CombatTurtle.class_desc() -> str
        Static method to return a description of the Combat Turtle AI.
        """

        return "A smart turtles."

    # -------------------------------------------------------------------------

    def class_shape():
        """CombatTurtle.class_shape() -> (int or tuple)
        Static method to define the Combat Turtle's shape image.

        The return value can be either an integer or a tuple of tuples.

        Returning an integer index selects one of the following preset shapes:
            0 -- arrowhead (also default in case of unrecognized index)
            1 -- turtle
            2 -- plow
            3 -- triangle
            4 -- kite
            5 -- pentagon
            6 -- hexagon
            7 -- star

        A custom shape can be defined by returning a tuple of the form
        (radius, angle), where radius is a tuple of radii and angle is a tuple
        of angles (in radians) describing the polar coordinates of a polygon's
        vertices. The shape coordinates should be given for a turtle facing
        east.
        """

        return 0

    # =========================================================================

    def setup(self):
        self.spd = 1.0
        self.turn_spd = 0.5
        self.pursuit_range = 0.75 * self.missile_range

# -------------------------------------------------------------------------

# -------------------------------------------------------------------------

    def step(self):
        health = self.health

        # 如果战斗力大于80，就直接攻击
        if health > 60:
            self.turn_towards()
            # 如果原地走了20步还没有攻击到对方，则主动攻击
            # print("x = ", self.x, "y = ", self.y)
            if self.distance() > 7 * self.missile_radius and self.time > 200 and health > 80:
                self.forward()
            elif health <= 80:
                self.forward(self.spd)
                self.right(0.3)
            else:
                self.backward()

        # 否则进行自卫模式
        elif health > 20:
            self.turn_towards()
            if self.distance() > 4 * self.missile_radius:
                self.backward()

        # 否则进行转右圈攻击模
        else:
            self.forward(self.spd)
            self.right(0.3)

        # if (self.can_shoot and abs(self.relative_heading_towards()) <= 5
        #         and self.line_of_sight()):
        if (self.can_shoot and abs(self.relative_heading_towards()) <= 7):
            self.shoot()


